package com.haochang.chunkvod.controller.activity.cash;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.SoftKeyboardUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.cash.CouponsDetailsAdapter;
import com.haochang.chunkvod.model.cash.CashData;
import com.haochang.chunkvod.model.cash.CouponInfo;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshBase;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshListView;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 *@author qintao
 *created at 2016/11/1 12:03
 * 卷详情
 */

public class CouponsDetailsActivity extends BaseActivity implements KeyBoardDialog.KeyBoardOnSureListener
{
	private TitleView titleView;
	private BaseEditText et_coupons_code;
	private BaseTextView tv_count;
	private PullToRefreshListView listView;
	private CouponsDetailsAdapter adapter;
	private List<CouponInfo> couponInfoList = new ArrayList<CouponInfo>();
	private CashData cashData;
	private boolean shouldRefreshData = false;//录入券成功后,setResult--刷新

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{
		cashData = new CashData(CouponsDetailsActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_coupons_details);
		Button btn_sure = (Button) findViewById(R.id.btn_sure);
		BaseTextView tv_minus_1 = (BaseTextView) findViewById(R.id.tv_minus_1);
		BaseTextView tv_plus_1 = (BaseTextView) findViewById(R.id.tv_plus_1);
		titleView = (TitleView) findViewById(R.id.titleView);
		listView = (PullToRefreshListView) findViewById(R.id.listView);
		et_coupons_code = (BaseEditText) findViewById(R.id.et_coupons_code);
		tv_count = (BaseTextView) findViewById(R.id.tv_count);
		tv_count.setOnClickListener(onBaseClickListener);
		btn_sure.setOnClickListener(onBaseClickListener);
		tv_minus_1.setOnClickListener(onBaseClickListener);
		tv_plus_1.setOnClickListener(onBaseClickListener);
	}

	@Override
	public void initViewsData()
	{
		titleView.setTitle(R.string.coupons_details).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				if (shouldRefreshData)
				{
					setResult(RESULT_OK, getIntent());
				}
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(CouponsDetailsActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
		adapter = new CouponsDetailsAdapter(CouponsDetailsActivity.this, R.layout.item_coupons_details_list_layout, couponInfoList);
		listView.setAdapter(adapter);
		listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<BaseListView>()
		{
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{
				//下拉刷新
				SoftKeyboardUtils.closeSoftKeyBoard(CouponsDetailsActivity.this,et_coupons_code);
				requestCouponList();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{

			}
		});
		requestCouponList();
	}

	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.tv_minus_1:
				//减1
				try
				{
					if (Integer.parseInt(tv_count.getText().toString()) <= 1)
						ToastUtils.showText(CouponsDetailsActivity.this, "不能小于1");
					else
						tv_count.setText(String.valueOf(Integer.parseInt(tv_count.getText().toString()) - 1));
				} catch (NumberFormatException e)
				{
					ToastUtils.showText(CouponsDetailsActivity.this, "数字异常");
				}
				break;
			case R.id.tv_plus_1:
				//加1
				try
				{
					tv_count.setText(String.valueOf(Integer.parseInt(tv_count.getText().toString()) + 1));
				} catch (NumberFormatException e)
				{
					ToastUtils.showText(CouponsDetailsActivity.this, "数字异常");
				}
				break;
			case R.id.tv_count:
				SoftKeyboardUtils.closeSoftKeyBoard(CouponsDetailsActivity.this, et_coupons_code);
				//数字弹框
				KeyBoardDialog keyBoardDialog = new KeyBoardDialog(CouponsDetailsActivity.this);
				keyBoardDialog.setKeyBoardOnSureListener(CouponsDetailsActivity.this);
				keyBoardDialog.show();
				break;
			case R.id.btn_sure:
				//录入
				if (TextUtils.isEmpty(et_coupons_code.getText()))
				{
					ToastUtils.showText(CouponsDetailsActivity.this, "请输入第一张券!");
					SoftKeyboardUtils.showSoftInput(CouponsDetailsActivity.this, et_coupons_code);
				} else
				{
					SoftKeyboardUtils.closeSoftKeyBoard(CouponsDetailsActivity.this, et_coupons_code);
					requestAddCoupon();
				}
				break;
			}
		}
	};

	@Override
	public void setValue(String value)
	{
		try
		{
			tv_count.setText(value);
		} catch (Exception e)
		{
			ToastUtils.showText(CouponsDetailsActivity.this, getResources().getString(R.string.error_max_number));
		}
	}

	private void requestAddCoupon()
	{
		cashData.requestAddCoupon(new CashData.ResultResponse()
		{
			@Override
			public void onRequestSuccess()
			{
				shouldRefreshData = true;
				ToastUtils.showText(CouponsDetailsActivity.this, "录入券成功!");
			}

			@Override
			public void onRequestFail(int errno, String errMsg)
			{
				ToastUtils.showText(CouponsDetailsActivity.this, !TextUtils.isEmpty(errMsg) ? errMsg : "录入券失败!");
			}
		});
	}

	/**
	 * 请求优惠期列表
	 */
	@SuppressWarnings("unchecked")
	private void requestCouponList()
	{
		cashData.requestCouponList(new CashData.ResultResponseList()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				listView.onRefreshComplete();
				if (null != list)
				{
					couponInfoList.clear();
					couponInfoList.addAll(list);
					adapter.replaceAll(couponInfoList);
				}
			}

			@Override
			public void onRequestFail(int errno, String errMsg)
			{
				listView.onRefreshComplete();
				ToastUtils.showText(CouponsDetailsActivity.this, !TextUtils.isEmpty(errMsg) ? errMsg : "获取券列表失败!");
				//以下测试
				CouponInfo info = new CouponInfo();
				info.setName("乐宝啤酒券");
				info.setType(0);
				info.setCount(56);
				info.setUnit("份");
				couponInfoList.add(info);
				info = new CouponInfo();
				info.setName("乐宝啤酒券**");
				info.setType(1);
				info.setCount(1111);
				info.setUnit("条");
				couponInfoList.add(info);
				adapter.replaceAll(couponInfoList);
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			if (shouldRefreshData)
			{
				setResult(RESULT_OK, getIntent());
				finish();
				animRightToLeft();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
